package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import io.renren.common.utils.LayuiData;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.SysRole1Service;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.sys.service.SysDept1Service;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 部门管理
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-08-17 11:17:41
 */
@RestController
@RequestMapping("sys/sysdept1")
public class SysDept1Controller extends  AbstractController{
    @Autowired
    private SysDept1Service sysDept1Service;
    @Autowired
    private SysUserService  sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysdept1:list")
    public R list(@RequestParam Map<String, Object> params){
        Long userId= ShiroUtils.getUserId();
        SysUserEntity user=sysUserService.getById(userId);
        Long deptId=user.getDeptId();
        SysDept1Entity sysDept1 = sysDept1Service.getById(deptId);
        String deptcode=sysDept1.getDeptcode();
        params.put("deptcode",deptcode);
        PageUtils page = sysDept1Service.queryPage(params);
        System.out.println("********* parsms is "+params + "*********");

        return R.ok().put("page", page);
    }

//    **
//            * 列表
//    */
    @RequestMapping("/list1")
    public R  list(){
        Map<String,Object> params= new HashMap<String, Object>();
        Long userId= ShiroUtils.getUserId();
        SysUserEntity user=sysUserService.getById(userId);
        Long deptId=user.getDeptId();
        //SysDept1Entity sysDept1Entity=sysDept1Service.getById(deptId);
//        String deptcodeself=sysDept1Entity.getDeptcode();
//        params.put("deptcode",deptcodeself);
//        System.out.println("deptcodeself is :" + deptcodeself + "*************");
        Long parentid=deptId;
        params.put("parentid",parentid);
        System.out.println("dept paretnidis :" + parentid + "*************");
        //Long deptid=getDeptId();
        List<SysDept1Entity> deptList1 = sysDept1Service.queryListbyParentId(params);
        return R.ok().put("list", deptList1);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{deptId}")
    @RequiresPermissions("sys:sysdept1:info")
    public R info(@PathVariable("deptId") Long deptId){
        SysDept1Entity sysDept1 = sysDept1Service.getById(deptId);

        return R.ok().put("sysDept1", sysDept1);
    }

    @RequestMapping("/info1")
    public R info1(){
        Long userId= ShiroUtils.getUserId();
        SysUserEntity user=sysUserService.getById(userId);
        Long deptId=user.getDeptId();
        SysDept1Entity sysDept1 = sysDept1Service.getById(deptId);
        System.out.println("deptid is "+sysDept1.getDeptId()+"parentid is "+sysDept1.getParentId()+"************");
        String parentid=sysDept1.getParentId().toString();
        //String deptcode=sysDept1.getDeptcode();
        return R.ok().put("parentid", parentid);
    }
    @RequestMapping("/info2")

    public R info2(){
        Long userId= ShiroUtils.getUserId();
        SysUserEntity user=sysUserService.getById(userId);
        Long deptId=user.getDeptId();
        SysDept1Entity sysDept1 = sysDept1Service.getById(deptId);
        System.out.println("deptid is "+sysDept1.getDeptId()+"parentid is "+sysDept1.getParentId()+"************");
        //Long parentId=sysDept1.getParentId();
        String deptcode=sysDept1.getDeptcode();
        return R.ok().put("deptcode", deptcode);
    }

    @RequestMapping("/save")
    @RequiresPermissions("sys:sysdept1:save")
    public R save(@RequestBody SysDept1Entity sysDept1){
        Long userId= ShiroUtils.getUserId();
        SysUserEntity sysUserEntity=sysUserService.getById(userId);
        Long deptid=getDeptId();
        SysDept1Entity sysDept1Entity=sysDept1Service.getById(deptid);
        String deptcodeself=sysDept1Entity.getDeptcode();
        Long parentid=getDeptId();
        Integer deptcount=-1;
        Map<String, Object> params=new HashMap<>();
        params.put("deptcode",deptcodeself);
        params.put("parentid",parentid);
        deptcount=sysDept1Service.queryCount(params);
        System.out.println("deptcodeself is " + deptcodeself + "deptcount is " + deptcount);
        List<Long> sysUserRoleEntityList=(List)sysUserRoleService.queryRoleIdList(userId);
        System.out.print("roleid is "+sysUserRoleEntityList.get(0)+"*****************");
        Integer roleId=sysUserRoleEntityList.get(0).intValue();
        String deptcode="";
        switch (roleId)
        {
            case 1:
                if (deptcount<=10){
                    deptcode=deptcodeself+"00"+deptcount;
                }
                else {
                    deptcode=deptcodeself+"0"+deptcount;
                 }
                break;
            case 2:
                if (deptcount<=10){
                    deptcode=deptcodeself+"0"+deptcount;
                }
                else{
                    deptcode=deptcodeself+deptcount;
                }
                break;
            default:
                deptcode="4104";
                break;
        }
        sysDept1.setParentId(deptid);
        sysDept1.setDeptcode(deptcode);
        sysDept1Service.save(sysDept1);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysdept1:update")
    public R update(@RequestBody SysDept1Entity sysDept1){
        ValidatorUtils.validateEntity(sysDept1);
        sysDept1Service.updateById(sysDept1);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysdept1:delete")
    public R delete(@RequestBody Long[] deptIds){
        sysDept1Service.removeByIds(Arrays.asList(deptIds));

        return R.ok();
    }

    /**
     * 角色列表
     */
    @RequestMapping("/select")
//    @RequiresPermissions("sys:sysrole1:select")
    public R select() {
        List<SysDept1Entity> list = sysDept1Service.list();

        return R.ok().put("list", list);
    }


        @RequestMapping("/dept")
        public String dept()
        {
//            LayuiData layuiData=new LayuiData();
            List<SysDept1Entity> list = sysDept1Service.list();
            int count = list.size();
//            layuiData.setCount(count);
//            layuiData.setCode(0);
//            layuiData.getMsg();
//            layuiData.setData(data);
            String json = "{\"code\":0,\"msg\":\"\",\"count\":"+count+",\"data\": " + JSON.toJSONString(list)+"}";
            return json;
        }
}



